package com.ascendere9.ff.pof.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.provider.Settings;

/**
 * @author: ThanhNH
 * @time: 9:59:31 AM - Jun 12, 2013
 */
public class LocationUtil {

	// ===================================================================
	// Constants
	// ===================================================================
	private static final String LOG_TAG = LocationUtil.class.getSimpleName();
	private static final int TWO_MINUTES = 1000 * 60 * 2;

	// ===================================================================
	// Fields
	// ===================================================================

	// ===================================================================
	// Constructors
	// ===================================================================

	// ===================================================================
	// Getter & Setter
	// ===================================================================

	// ===================================================================
	// Methods from BaseClass & Interfaces
	// ===================================================================

	// ===================================================================
	// Methods
	// ===================================================================
	public static Location getCurrentLocation(Context pContext) {

		Location gpsLocation = null;
		Location networkLocation = null;
		LocationManager locationManager = (LocationManager) pContext
				.getSystemService(pContext.LOCATION_SERVICE);

		// getting GPS status
		boolean isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
		// getting network status
		boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

		if (isGPSEnabled || isNetworkEnabled) {
			// First get location using GPS Services
			if (isGPSEnabled) {
				if (locationManager != null) {
					gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
				}
			}
			// get location from Network Provider
			if (isNetworkEnabled) {
				if (locationManager != null) {
					networkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
				}
			}
			if (gpsLocation == null || networkLocation == null) {
				return (gpsLocation != null) ? gpsLocation : networkLocation;
			}
			return (LocationUtil.isBetterLocation(gpsLocation, networkLocation)) ? gpsLocation : networkLocation;
		}
		return null;
	}

	private static boolean isBetterLocation(Location location, Location currentBestLocation) {

		// Check whether the new location fix is newer or older
		long timeDelta = location.getTime() - currentBestLocation.getTime();
		boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
		boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
		boolean isNewer = timeDelta > 0;

		// If it's been more than two minutes since the current location, use
		// the new location
		// because the user has likely moved
		if (isSignificantlyNewer) {
			return true;
			// If the new location is more than two minutes older, it must be
			// worse
		} else if (isSignificantlyOlder) {
			return false;
		}

		// Check whether the new location fix is more or less accurate
		int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
		boolean isLessAccurate = accuracyDelta > 0;
		boolean isMoreAccurate = accuracyDelta < 0;
		boolean isSignificantlyLessAccurate = accuracyDelta > 200;

		// Check if the old and new location are from the same provider
		boolean isFromSameProvider = LocationUtil.isSameProvider(location.getProvider(), currentBestLocation.getProvider());

		// Determine location quality using a combination of timeliness and
		// accuracy
		if (isMoreAccurate) {
			return true;
		} else if (isNewer && !isLessAccurate) {
			return true;
		} else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
			return true;
		}
		return false;
	}

	/** Checks whether two providers are the same */
	private static boolean isSameProvider(String provider1, String provider2) {
		if (provider1 == null) {
			return provider2 == null;
		}
		return provider1.equals(provider2);
	}

	
	public static void showSettingsAlert(final Context pContext) {
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(pContext);

		// Setting Dialog Title
		alertDialog.setTitle("GPS is settings");

		// Setting Dialog Message
		alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

		// Setting Icon to Dialog
		// alertDialog.setIcon(R.drawable.delete);

		// On pressing Settings button
		alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
				pContext.startActivity(intent);
			}
		});

		// on pressing cancel button
		alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.cancel();
			}
		});

		// Showing Alert Message
		alertDialog.show();
	}
	// ===================================================================
	// Methods for SubClass
	// ===================================================================

	// ===================================================================
	// Inner and Anonymous Classes
	// ===================================================================
}
